﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using COFE.Core.Lister;
using COFE.Core;
using System.Drawing;
using COFE.IO.FSIEX.Utils;
using COFE.Core.Utils;

namespace COFE.IO.FSIEX
{
    public class FileSystemExPropertyProvider : PropertyProviderBase<FileSystemInfoEx>
    {
        #region Constructor
        public FileSystemExPropertyProvider(string listerKey, string rootPath, FileSystemInfoEx entry)
            : base(entry)
        {
            registerProperties(listerKey, rootPath, entry is FileInfoEx);
        }
        #endregion

        #region Methods
        public override void Refresh()
        {
            EmbeddedEntry.Refresh();
            base.Refresh();
        }

        private void registerProperties(string listerKey, string rootPath, bool isFile)
        {
            base.RegisterMethod(FileSystemInfoProperties.FullName, (e, p) => { return e.FullName.TrimEnd('\\'); });
            base.RegisterMethod(FileSystemInfoProperties.FullParseName, (e, p) =>
            {
                return COFEPath.FormatFullParseName(listerKey, e.FullName.Replace(rootPath, "",
                  StringComparison.InvariantCultureIgnoreCase).TrimEnd('\\'));
            });
            //{ return _rootPath == null ? e.FullName : IOTools.GetRelativePath(e, rootDir); }); 
            //IOTools.GetRelativePath do not support Guid path.
            base.RegisterMethod(FileSystemInfoProperties.ParentParseName, (e, p) =>
            {
                if (e.Parent == null)
                    return null;
                return COFEPath.FormatFullParseName(listerKey, e.Parent.FullName.Replace(rootPath, "",
                StringComparison.InvariantCultureIgnoreCase).TrimEnd('\\'));
            }
            );

            base.RegisterMethod(FileSystemInfoProperties.Name, (e, p) => { return e.Name; });
            base.RegisterMethod(FileSystemInfoProperties.Label, (e, p) => { return e.Label; });

            base.RegisterMethod(FileSystemInfoProperties.IsExists, (e, p) => { return e.Exists; });
            base.RegisterMethod(FileSystemInfoProperties.IsFileSystem, (e, p) => { return true; });
            base.RegisterMethod(FileSystemInfoProperties.FileAttributes, (e, p) => { return (COFE.Core.FileAttributes)(int)e.Attributes; });

            base.RegisterMethod(FileSystemInfoProperties.COFEAttributes, (e, p) =>
               {
                   return (e is DirectoryInfoEx) ? COFEAttributes.FileSystem | COFEAttributes.SubEntriesCanChange | COFEAttributes.Browsable :
                     COFEAttributes.FileSystem;
               });

            base.RegisterMethod(FileSystemInfoProperties.CreationTimeUtc,
                (e, p) => { return e.CreationTimeUtc; });
            base.RegisterMethod(FileSystemInfoProperties.LastAccessTimeUtc,
                (e, p) => { return e.LastAccessTimeUtc; });
            base.RegisterMethod(FileSystemInfoProperties.LastWriteTimeUtc,
                (e, p) => { return e.LastWriteTimeUtc; });

            if (isFile)
            {
                base.RegisterMethod(FileInfoProperties.Length, (e, p) => { return (e as FileInfoEx).Length; });
            }
            else
            {
                string parseName = base.GetProperty(FileSystemInfoProperties.FullParseName).ValueAsString;
                if (parseName == "")
                    base.RegisterMethod(DirectoryInfoProperties.DirectoryType, (e, p) => { return COFE.Core.DirectoryTypeEnum.dtVolume; });
                else
                    base.RegisterMethod(DirectoryInfoProperties.DirectoryType,
                        (e, p) => { return (COFE.Core.DirectoryTypeEnum)(int)(e as DirectoryInfoEx).DirectoryType; });
                base.RegisterMethod(DirectoryInfoProperties.IsFileBasedFS, (e, p) => { return false; });
            }
            //base.RegisterMethod(IconProperties.SmallIcon, (e,p) =>
            //{
            //    return _exiconv.GetIcon(e, IconSize.small, !isFile, true).EnsureBitmapSize(IconProperties.SmallIcon);
            //});
            //base.RegisterMethod(IconProperties.Icon, (e,p) =>
            //{
            //    return _exiconv.GetIcon(e, IconSize.large, !isFile, true).EnsureBitmapSize(IconProperties.Icon);
            //});
            //base.RegisterMethod(IconProperties.ExtraLargeIcon, (e,p) =>
            //{
            //    return _exiconv.GetIcon(e, IconSize.extraLarge, !isFile, true).EnsureBitmapSize(IconProperties.ExtraLargeIcon);
            //});
            //base.RegisterMethod(IconProperties.JumboIcon, (e,p) =>
            //{
            //    return _exiconv.GetIcon(e, IconSize.jumbo, !isFile, true).EnsureBitmapSize(IconProperties.JumboIcon);
            //});


        }

        #endregion

        #region Data


        #endregion

        #region Public Properties

        //private static ExIconExtractor _exiconv = new ExIconExtractor();

        #endregion

    }
}
